package chart;

import java.awt.Color;
import java.util.Iterator;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;

public class XYAreaChartDemo2 extends ApplicationFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TimeSeries series;
	private TimeSeries series1;
	
	public XYAreaChartDemo2(String title, Vector<Short> channel1,Vector<Short> channel2,String name1,String name2) {

		super(title);
		this.series = new TimeSeries(name1, Millisecond.class);
		this.series1 = new TimeSeries(name2, Millisecond.class);

		JFreeChart chart = createChart(createDataset(channel1,name1,channel2,name2));

		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		setContentPane(chartPanel);

	}



	private void createSeries(Vector<Short> channel, String name,Vector<Short> channel1, String name1){
		
		Millisecond m1 = new Millisecond(0,0,0,0,25,11,2008);
		
		Iterator<Short> iterator1 = channel.iterator();
		//Millisecond m = new Millisecond(0,0,0,0,25,11,2008);
		Iterator<Short> iterator = channel1.iterator();

		while(iterator1.hasNext() && iterator.hasNext()){
			this.series.addOrUpdate(m1.next() ,iterator1.next());
			this.series1.addOrUpdate(m1.previous() ,iterator.next());
			m1 = (Millisecond) m1.next();
		}
		
		
		
		
		

		//while(iterator.hasNext()){
		//	this.series1.addOrUpdate(m ,iterator.next());
		//	m = (Millisecond) m.next();
		//}
		
		
	}

	private TimeSeriesCollection createDataset(Vector<Short> channel1, String name1,Vector<Short> channel2, String name2) {

		this.createSeries(channel1,name1,channel2,name2);		
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(this.series);
		dataset.addSeries(this.series1);
		
		DateAxis domain = new DateAxis("Time");
		NumberAxis range = new NumberAxis("Memory");
		
		XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
		renderer.setSeriesPaint(0, Color.red);
		renderer.setSeriesPaint(1, Color.green);
		
		XYPlot plot = new XYPlot(dataset, domain, range, renderer);
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		
		domain.setAutoRange(true);
		domain.setLowerMargin(0.0);
		domain.setUpperMargin(0.0);
		domain.setTickLabelsVisible(true);
		
		return dataset;
	}

	private JFreeChart createChart(XYDataset dataset) {
		
		
		//JFreeChart chart1 = ChartFactory.createXYLineChart(title, xAxisLabel, yAxisLabel, dataset, orientation, legend, tooltips, urls)
		JFreeChart chart = ChartFactory.createXYLineChart(
				"Loss Comparation",
				"Time", "Amplitude",
				dataset,
				PlotOrientation.VERTICAL,
				true,  // legend
				true,  // tool tips
				false  // URLs
		);
		
		
		XYPlot plot = chart.getXYPlot();
		
		XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
		renderer.setSeriesPaint(0, Color.red);
		renderer.setSeriesPaint(1, Color.green);

		ValueAxis domainAxis = new DateAxis("Time");
		domainAxis.setLowerMargin(0.0);
		domainAxis.setUpperMargin(0.0);
		plot.setDomainAxis(domainAxis);
		plot.setForegroundAlpha(0.5f);  


		return chart;      
	}


	public void configurateGraphic() {
		
	}
	


}
